package com.meiyu.live.ui.main.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.meiyu.live.R;
import com.meiyu.live.comm.UserConfig;
import com.meiyu.live.popj.Hookup;
import com.meiyu.live.ui.main.AnchorDetailActivity;

public class MyHookItemView extends FrameLayout implements View.OnClickListener {

    private TextView tv_progress, tv_name, tv_detail, tv_level, tv_signature;
    ImageView iv_avatar;
    private ProgressBar progressBar;

    private ImageLoader imageLoader;
    private DisplayImageOptions options;

    private Hookup bean;
    private OnLiveItemViewClickListener listener;

    public interface OnLiveItemViewClickListener {
        void onClickLike(Hookup bean);
    }

    public MyHookItemView(Context context) {
        super(context);
        init(context);
    }

    public MyHookItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MyHookItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.adapter_myhook_item, this);

        iv_avatar = (ImageView) findViewById(R.id.iv_avatar);
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_level = (TextView) findViewById(R.id.tv_level);
        tv_signature = (TextView) findViewById(R.id.tv_signature);
        tv_detail = (TextView) findViewById(R.id.tv_detail);
        imageLoader = ImageLoader.getInstance();
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.image_loading)          // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.drawable.image_loading)  // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.image_loading)       // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)                          // 设置下载的图片是否缓存在SD卡中
//                .displayer(new FadeInBitmapDisplayer(200))// 图片加载好后渐入的动画时间
                .build();

        tv_progress = (TextView) findViewById(R.id.tv_progress);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        setOnClickListener(this);
    }

    public void setData(int type, Hookup bean) {
        this.bean = bean;
        int progress = bean.getPercent_shares();
        progressBar.setProgress(progress > 100 ? 100 : progress);
        tv_progress.setText(progress + "%");

        imageLoader.displayImage(UserConfig.getInstance().getPersonalInfo().getAvatar(), iv_avatar, options);
        tv_name.setText(UserConfig.getInstance().getPersonalInfo().getName());
        String level = UserConfig.getInstance().getPersonalInfo().getLevel();
        tv_level.setText(TextUtils.isEmpty(level) ? "1" : level);
        if (TextUtils.isEmpty(bean.getDescription())) {
            tv_signature.setText("暂无介绍");
        } else {
            tv_signature.setText(bean.getDescription());
        }
        if (type == 1) {
            tv_detail.setText("待审核");
            progressBar.setVisibility(View.GONE);
            tv_progress.setVisibility(View.GONE);
        } else if (bean.getOver_plus_days() <= 0) {
            tv_detail.setText("已结束");
        } else {
            tv_detail.setText("已有" + bean.getBought_persons() + "人邀约" + "  剩余" + bean.getOver_plus_days() + "天");
        }

    }

    @Override
    public void onClick(View view) {
        if (bean != null) {

            if (view == this) {
                AnchorDetailActivity.start(getContext(), bean);
            }
        }
    }

    public void setOnLiveItemViewClickListener(OnLiveItemViewClickListener listener) {
        this.listener = listener;
    }
}
